clear  all
set maxvar 10000
set more off

* ================================================
* written by Byungkyu Lee (bl2474@columbia.edu)
* Date : 10/27/16
* to reproduce Table 1 and Table 2, Table B2 in the Appendix B
* Lee, Byungkyu and Peter Bearman, 2016, "Important Matters in Political Context", Sociological Science
* ================================================

* set up the working directory in your computer
cd 

* load the analytic data
use imp_combine, clear 

* party identification 
gen rp1 = r_partyid == 3 if ~missing(r_partyid)
label var rp1 "Party ID = Independent"

* set up the survey designs 
svyset sampcode [pw= wt]
xtset svydate2 

* -------------- Table 1 : descriptive statistics
	gen v_name = ""
	
	foreach y of numlist 1985 1987 2000 2004 2006 2007 2010 {
	cap: gen v_mean`y' = .
	
	* ------- proportion of each size
	forvalues i = 1/7 {
		replace v_name = "network size = `i'" in `i'
		if `y' == 2006 {
			qui mean ns`i' [pw=wt] if year == 2006 & module == 1
		} 
		else if `y' == 2007 {
			qui mean ns`i' [pw=wt] if year == 2006 & module == 2
		} 
		else {
			qui mean ns`i' [pw=wt] if year == `y'
		}
		replace v_mean`y' = _b[ns`i'] in `i'
	}
	
	* -------- average network size
		if `y' == 2006 {
			qui mean n_size6 [pw=wt] if year == 2006 & module == 1
		} 
		else if `y' == 2007 {
			qui mean n_size6 [pw=wt] if year == 2006 & module == 2
		} 
		else {
			qui mean n_size6 [pw=wt] if year == `y'
		}
		
		replace v_name = "Mean Network Size" in 8
		replace v_mean`y' = _b[n_size6] in 8 
	
	* -------- sd of network size 
		if `y' == 2006 {
			qui sum n_size6 if year == 2006 & module == 1
		} 
		else if `y' == 2007 {
			qui sum n_size6 if year == 2006 & module == 2
		} 
		else {
			qui sum n_size6 if year == `y'
		}
	
		replace v_name = "SD Network Size" in 9
		replace v_mean`y' = r(sd) in 9 
		
		replace v_name = "Total N" in 10
		replace v_mean`y' = r(N) in 10
	
	* -------- N of days
		if `y' == 2006 {
			qui sum svydate2 if year == 2006 & module == 1 & ~missing(n_size)
		} 
		else if `y' == 2007 {
			qui sum svydate2 if year == 2006 & module == 2 & ~missing(n_size)
		} 
		else {
			qui sum svydate2 if year == `y' & ~missing(n_size)
		}
		replace v_name = "N of days" in 11
		replace v_mean`y' = r(max)-r(min)+1 in 11
	
	* -------- variance of dayily mean
		if `y' == 2006 {
			qui xtpoisson n_size , normal irr, if year == 2006 & module == 1
		} 
		else if `y' == 2007 {
			qui xtpoisson n_size , normal irr, if year == 2006 & module == 2
		} 
		else {
			qui xtpoisson n_size , normal irr, if year == `y'
		}
		replace v_name = "daily variances" in 12
		replace v_mean`y' = e(sigma_u) in 12
		replace v_name = "chi-square" in 13
		replace v_mean`y' = e(chi2_c) in 13
		replace v_name = "p-value" in 14	
		replace v_mean`y' = chi2tail(1, e(chi2_c))/2 in 14
	
	* -------- mean of network size : all
		if `y' == 2006 {
			qui mean n_size [pw=wt] if year == 2006 & module == 1
		} 
		else if `y' == 2007 {
			qui mean n_size [pw=wt] if year == 2006 & module == 2
		} 
		else {
			qui mean n_size [pw=wt] if year == `y'
		}
		replace v_name = "Mean Network Size (all)" in 16
		replace v_mean`y' = _b[n_size] in 16
	
	* -------- mean of network size : 4
		if `y' == 2006 {
			qui mean n_size4 [pw=wt] if year == 2006 & module == 1
		} 
		else if `y' == 2007 {
			qui mean n_size4 [pw=wt] if year == 2006 & module == 2
		} 
		else {
			qui mean n_size4 [pw=wt] if year == `y'
		}
		replace v_name = "Mean Network Size (4)" in 17
		replace v_mean`y' = _b[n_size4] in 17
	}
	
	
	outsheet v_* using Table1.csv, comma replace, if v_name != "" 
	drop v_* 
	
* --------- check interview date;
	foreach y of numlist 1985 1987 2000 2004 2006 2010 {
		sum svydate2 if ~missing(n_size) & year == `y', format
	}

* --------- mean of network size across partisanships for Figure 3
	svy: mean n_size6, over(rp1) , if year == 1985 // 2.99 | 2.62 | ns 
	svy: mean n_size6, over(rp1) , if year == 1987 // 2.52 | 2.35 | ns 
	svy: mean n_size6, over(rp1) , if year == 2000 // 2.39 | 1.56 | *
	svy: mean n_size6, over(rp1) , if year == 2004 // 2.25 | 1.53 | *
	svy: mean n_size6, over(rp1) , if year == 2010 // 2.51 | 2.07 | * 

* ----------- Table 2/B2; differences of network size between partisans and nonpartisans across itme
estimates clear
eststo clear
eststo: svy: zip n_size r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1985.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1985.year), if (year == 2004 | year == 1985) & rp1 == 1
eststo: svy: zip n_size r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1987.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1987.year), if (year == 2004 | year == 1987) & rp1 == 1
eststo: svy: zip n_size4 r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2000.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2000.year), if (year == 2004 | year == 2000) & rp1 == 1
eststo: svy: zip n_size r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2010.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2010.year), if (year == 2004 | year == 2010) & rp1 == 1
esttab * using Table2a.csv, csv replace star (+ 0.1 * 0.05 ** 0.01 *** 0.001) nogap label b(%9.3f) se 

estimates clear
eststo clear
eststo: svy: zip n_size r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1985.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1985.year), if (year == 2004 | year == 1985)
eststo: svy: zip n_size r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1987.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b1987.year), if (year == 2004 | year == 1987)
eststo: svy: zip n_size4 r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2000.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2000.year), if (year == 2004 | year == 2000)
eststo: svy: zip n_size r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2010.year,inflate(r_age r_female r_educ i.r_race r_married i.r_wrkstat r_nchilds c.rp1##b2010.year), if (year == 2004 | year == 2010)
esttab * using Table2b.csv, csv replace star (+ 0.1 * 0.05 ** 0.01 *** 0.001) nogap label b(%9.3f) se 



